package com.wgj.android.ui.activity;
import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.alibaba.fastjson.JSONObject;
import com.jiaai.xui.XUI;
import com.jiaai.xui.adapter.XRecyclerViewAdapter;
import com.jiaai.xui.adapter.XViewHolder;
import com.jiaai.xui.adapter.decoration.DividerDecoration;
import com.jiaai.xui.utils.XAppUtils;
import com.jiaai.xui.utils.XDensityUtils;
import com.jiaai.xui.utils.XFormatTimeUtils;
import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.ListHttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.XAlertMsgDialog;
import com.jiaai.xui.view.widget.loadingview.XLoadingView;
import com.wgj.android.R;
import com.wgj.android.accesslibility.config.WXConfginUtil;
import com.wgj.android.accesslibility.config.WXQiangHongBaoConfig;
import com.wgj.android.accesslibility.service.WGJAccessServier;
import com.wgj.android.entity.BonusHistory;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.utils.AccessibilityUtil;
import com.wgj.android.utils.CacheUtils;
import com.wgj.android.utils.WXAccessUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
 * ========================================
 * 作    者： 佳爱网络有限公司(wwww.jiaailove.com)
 * 版    本： 1.0
 * 创建日期：  2017/11/14
 * 描    述：
 * 修订历吏：
 * =========================================
 */
public class WXHongBaoActivity extends AppCompatActivity  implements SwipeRefreshLayout.OnRefreshListener{

    private  DecimalFormat    df   = new DecimalFormat("######0.00");
    @BindView(R.id.loadingView)
    XLoadingView loadingView;


    @BindView(R.id.historyListView)
    RecyclerView recyclerView;

    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;

    @BindView(R.id.todayMoney)
    TextView todyMoney;

    @BindView(R.id.historyMoney)
    TextView historyMoney;


    @BindView(R.id.startAndStopBut)
    Button startAndStopBut;

    private List<BonusHistory> historys=new ArrayList<>();

    private XRecyclerViewAdapter<BonusHistory> adapter;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.hong_bao_activity);
        ButterKnife.bind(this);
        initView();
    }

    private  void initView(){
        adapter=new XRecyclerViewAdapter<BonusHistory>(recyclerView,historys,R.layout.hong_bao_history_item) {
             @Override
             protected void bindData(XViewHolder holder, BonusHistory bonusHistory, int i) {
                 holder.setText(R.id.nickName,bonusHistory.getName());
                 holder.setText(R.id.time, XFormatTimeUtils.getTimeSpanByNow1(bonusHistory.getDatetime()));
                 holder.setText(R.id.money, String.valueOf(bonusHistory.getAmount())+"元");
             }
         };
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        refreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light,android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        refreshLayout.setProgressViewOffset(false, 0, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
                        .getDisplayMetrics()));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerDecoration(XUI.getColor(R.color.divider), XDensityUtils.dp2px(1)));
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onRefresh() {
        XHttp.obtain().get(WGJAPI.BASEURL.concat("hb/myhis?mId=").concat(CacheUtils.getMember().getM_id()), null, new ListHttpCallBack<BonusHistory>() {
            @Override
            public void onSuccess(List<BonusHistory> list) {
                historys.clear();
                if(list==null||list.size()==0){
                    loadingView.showEmpty("暂无红包数据");
                }else {
                  historys.addAll(list);
                  adapter.notifyDataSetChanged();
                  loadingView.showContent();
                }
                refreshLayout.setRefreshing(false);
            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                if(b){
                    loadingView.showNoNetwork();
                }else {
                    loadingView.showError();
                }
                refreshLayout.setRefreshing(false);
            }
        });
    }


    private void  total(){
        XHttp.obtain().get(WGJAPI.BASEURL.concat("hb/bonus?mId=").concat(CacheUtils.getMember().getM_id()),null, new HttpCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                JSONObject ob=JSONObject.parseObject(s);
                historyMoney.setText(ob.getString("all").concat("元"));
                todyMoney.setText(ob.getString("today").concat("元"));
            }
            @Override
            public void onFailed(int i, String s, boolean b) {

            }
        });


    }

    @OnClick(R.id.startAndStopBut)
    public void onStartAndStopButClick() {
        if (WXConfginUtil.isOpenQhb()) {
                WXConfginUtil.closeQhb();
                startAndStopBut.setBackground(XUI.getDrawable(R.drawable.red_circle_bg));
                startAndStopBut.setTag(false);
                startAndStopBut.setTextColor(Color.WHITE);
                startAndStopBut.setText("停止");
        }else {
            if (WXAccessUtil.checkAccessRunAmbient(this)) {
                startAndStopBut.setBackground(XUI.getDrawable(R.drawable.white_circle_bg));
                startAndStopBut.setTextColor(Color.RED);
                startAndStopBut.setText("开始");
                startAndStopBut.setTag(true);
                WXConfginUtil.openQhb();
            }
        }

    }



    @OnClick(R.id.exit)
    public void onExit(){
       finish();
    }

    @OnClick(R.id.settingBut)
    public void onSettingClick(){
        Intent intent=new Intent(getApplication(),WXHongBaoConfigActivity.class);
        startActivity(intent);
    }

    /** 打开通知栏设置*/
    @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
    private void openNotificationServiceSettings() {
        try {
            Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
            startActivity(intent);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
         super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        total();
        onRefresh();
        if(WGJAccessServier.isRun()){
            WXConfginUtil.openQhb();
            startAndStopBut.setBackground(XUI.getDrawable(R.drawable.red_circle_bg));
            startAndStopBut.setTag(false);
            startAndStopBut.setTextColor(Color.WHITE);
            startAndStopBut.setText("停止");
        }else {
            WXConfginUtil.closeQhb();
            startAndStopBut.setBackground(XUI.getDrawable(R.drawable.white_circle_bg));
            startAndStopBut.setTextColor(Color.RED);
            startAndStopBut.setText("开始");
            startAndStopBut.setTag(true);
        }

    }
}
